import collections
class Solution(object):
    def minWastedSpace(self, packages, boxes):
        st = collections.Counter(packages)
        total_packges = len(packages)
        max_num = 10 ** 5 + 1
        p = [0] * max_num
        t = [0] * max_num
        count = 0
        base = 10 ** 9 + 7
        for i in range(1, max_num):
            p[i] = p[i - 1] + count
            count += st[i]
            t[i] = count
        min_ans = float('inf')
        for box in boxes:
            box.sort()
            if t[box[-1]] < total_packges:
                continue
            total_ans = 0
            for i in range(1, len(box)):
                total_ans += t[box[i - 1]] * (box[i] - box[i - 1])
            now_ans = p[box[-1]] - total_ans
            min_ans = min(min_ans, now_ans)
        if min_ans == float('inf'):
            return -1
        return min_ans % base


data = Solution()
packages = [3,5,8,10,11,12]
boxes = [[12],[11,9],[10,5,14]]
print(data.minWastedSpace(packages, boxes))